package com.example.demo.controller;


import com.example.demo.entity.Exam;
import com.example.demo.service.IExamService;
import com.example.demo.utils.AjaxJson;
import com.example.demo.vo.examAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 考试表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-11-10
 */
@RestController
@RequestMapping("/exam")
public class ExamController {
    
    @Autowired
    private IExamService examService;
    
    @RequestMapping("/add")
    public AjaxJson add(@RequestBody Exam exam) {
        return examService.save(exam) ? AjaxJson.getSuccess("添加成功") : AjaxJson.getError("添加失败");
    }
    
    @RequestMapping("/delete")
    public AjaxJson delete(@RequestParam Integer id) {
        return examService.removeById(id) ? AjaxJson.getSuccess("删除成功") : AjaxJson.getError("删除失败");
    }
    
    @RequestMapping("/update")
    public AjaxJson update(@RequestBody Exam exam) {
        return examService.updateById(exam) ? AjaxJson.getSuccess("修改成功") : AjaxJson.getError("修改失败");
    }
    
    @RequestMapping("/query")
    public AjaxJson query() {
        return AjaxJson.getPageData(examService.count(), examService.list());
    }

    @RequestMapping("/query/teacherId")
    public AjaxJson queryByTeacherId(@RequestParam Integer teacherId) {
        return examService.getByTeacherId(teacherId);
//        return AjaxJson.getPageData(examService.count(), examService.list());
    }


    @RequestMapping("/query/id")
    public AjaxJson query(@RequestParam Integer id) {
        return AjaxJson.getSuccessData(examService.getById(id));
    }

    @RequestMapping("/updata/teacher")
    public AjaxJson updataTeacher(@RequestBody examAddVo examAddVo) {
        return examService.updataByExamAddVo(examAddVo);
    }

    @RequestMapping("/add/teacher")
    public AjaxJson addTeacher(@RequestBody examAddVo examAddVo) {
        return examService.saveByExamAddVo(examAddVo);
    }




    @RequestMapping("/query/studentId")
    public AjaxJson queryByStudentId() {
        return examService.getByStudentId();
    }

    /**
     * 学生查询单个考试
     */
    @RequestMapping("/query/exStId")
    public AjaxJson queryByExStId(@RequestParam Integer examId) {
        return examService.getByExStId(examId);
    }
}